/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.harvest.tei.svn;

import cz.mzk.oai.metadataRepository.repository.Identifier;
import cz.mzk.oai.metadataRepository.sysnoSignatureMapping.Mapping;
import cz.mzk.oai.metadataRepository.sysnoSignatureMapping.MappingsLoader;
import cz.mzk.oai.metadataRepository.sysnoSignatureMapping.XmlMappingsLoader;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class SignatureSysnoMapper {

    private static final Logger logger = Logger.getLogger(SignatureSysnoMapper.class.getName());
    private final Map<String, Identifier> signatureIdMap = new HashMap<String, Identifier>();

    public SignatureSysnoMapper(File mappingFile) {
        MappingsLoader loader = new XmlMappingsLoader(mappingFile);
        processMappings(loader);
    }

    private void processMappings(MappingsLoader loader) {
        List<Mapping> mappings = loader.loadMappings();
        for (Mapping mapping : mappings) {
            addToMap(mapping);
        }
    }

    private void addToMap(Mapping mapping) {
        String signature = mapping.getSignature();
        Identifier idNew = mapping.getId();
        Identifier idPresent = signatureIdMap.get(signature);
        if (idPresent != null) {
            logger.log(Level.WARNING, "id already present for signarure {0}, found {1}, adding {2}", new Object[]{signature, idPresent.toString(), idNew.toString()});
        }
        signatureIdMap.put(signature, idNew);
    }

    public Identifier getIdentifier(String signature){
        return signatureIdMap.get(signature);
    }

    public Collection<Identifier> allIdentifiers(){
        return signatureIdMap.values();
    }

    public Set<String> allSignatures(){
        return signatureIdMap.keySet();
    }
}
